package com.mt.roll.process.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mt.roll.core.api.Assert;
import com.mt.roll.core.base.BaseService;
import com.mt.roll.core.dal.entity.*;
import com.mt.roll.core.dal.service.*;
import com.mt.roll.core.errorcode.ApiErrorCode;
import com.mt.roll.core.params.EnabledParam;
import com.mt.roll.core.params.IdListParam;
import com.mt.roll.core.params.IdParam;
import com.mt.roll.core.utils.CodeGeneratorHelper;
import com.mt.roll.core.vo.IdListVo;
import com.mt.roll.core.vo.IdVo;
import com.mt.roll.core.vo.PageVo;
import com.mt.roll.core.vo.R;
import com.mt.roll.process.mapper.ProcessFlowVoMapper;
import com.mt.roll.process.params.ProcessFlowParam;
import com.mt.roll.process.params.ProcessFlowQueryParam;
import com.mt.roll.process.service.ProcessFlowService;
import com.mt.roll.process.vo.BomVo;
import com.mt.roll.process.vo.ProcessFlowVo;
import com.mt.roll.process.vo.SubstrateVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 工艺服务实现类
 *
 * @author lgh
 * @date 2020/12/29
 * @since 1.0
 */
@Service
@Transactional
public class ProcessFlowServiceImpl extends BaseService implements ProcessFlowService {

    @Resource
    private ProcessFlowServiceBiz processFlowServiceBiz;
    @Resource
    private ProcessFlowEquipmentServiceBiz processFlowEquipmentServiceBiz;
    @Resource
    private ProcessFlowSubstrateRelaServiceBiz processFlowSubstrateRelaServiceBiz;
    @Resource
    private ProcessFlowVoMapper processFlowVoMapper;
/*    @Resource
    private SubstrateServiceBiz substrateServiceBiz;
    @Resource
    private BomServiceBiz bomServiceBiz;*/
    /**
     * 获取详情
     *
     * @param idParam 主键参数
     * @return 工艺
     */
    @Override
    public R<ProcessFlowVo> get(IdParam idParam) {
        Assert.notNull(ApiErrorCode.INVALID_PARAMETER,idParam.getId());
        ProcessFlowVo processFlowVo = processFlowVoMapper.getProcessFlowVoById(idParam.getId());
        return successful(processFlowVo);
    }

    /**
     * 获取工艺列表
     *
     * @param processFlowQueryParam 工艺查询参数
     * @return 工艺列表
     */
    @Override
    public R<List<ProcessFlowVo>> list(ProcessFlowQueryParam processFlowQueryParam) {
        List<ProcessFlowVo> processFlowVos = processFlowVoMapper.getProcessFlowVoList(processFlowQueryParam.getKey());
        return successful(processFlowVos);
    }

    /**
     * 获取工艺分页列表
     *
     * @param processFlowQueryParam 工艺查询参数
     * @return 工艺分页列表
     */
    @Override
    public R<PageVo<ProcessFlowVo>> page(ProcessFlowQueryParam processFlowQueryParam) {
/*        QueryWrapper<ProcessFlow> wrapper=new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(processFlowQueryParam.getName()),ProcessFlow.NAME,processFlowQueryParam.getName());
        Page<ProcessFlow> page = processFlowServiceBiz.page(new Page<>(processFlowQueryParam.getCurrent(), processFlowQueryParam.getSize()), wrapper);
        return successful(new PageVo<>(page,ProcessFlowVo.class));*/
        IPage<ProcessFlowVo> processFlowVos = processFlowVoMapper.getProcessFlowVoList(new Page<>(processFlowQueryParam.getCurrent(), processFlowQueryParam.getSize()), processFlowQueryParam.getKey());
        return successful(new PageVo<>(processFlowVos,ProcessFlowVo.class));
    }

    /**
     * 获取xx树列表
     *
     * @param processFlowQueryParam xx查询参数
     * @return xx树列表
     */
    @Override
    public R<List<ProcessFlowVo>> tree(ProcessFlowQueryParam processFlowQueryParam) {
        return null;
    }

    /**
     * 新增工艺
     *@TODO
     * @param processFlowParam 工艺参数
     * @return 主键
     */
    @Override
    public R<IdVo> add(ProcessFlowParam processFlowParam) {
        QueryWrapper<ProcessFlow> wrapper=new QueryWrapper<>();
        wrapper.eq(ProcessFlow.NAME,processFlowParam.getName()).or().eq(ProcessFlow.CODE,processFlowParam.getCode());
        Assert.eqZero(processFlowServiceBiz.count(wrapper),"工艺名称或编码已存在");
        ProcessFlow processFlow=new ProcessFlow();
        BeanUtils.copyProperties(processFlowParam,processFlow);
        setCommonField(processFlow);
        processFlowServiceBiz.save(processFlow);
        return successful(IdVo.builder().id(processFlow.getId()).build());
    }

    /**
     * 更新工艺
     * @TODO
     * @param processFlowParam 工艺参数
     * @return 主键
     */
    @Override
    public R<IdVo> update(ProcessFlowParam processFlowParam) {
        ProcessFlow processFlow = processFlowServiceBiz.getById(processFlowParam.getId());
        if (!processFlow.getName().equals(processFlowParam.getName())){
            QueryWrapper<ProcessFlow> wrapper=new QueryWrapper<>();
            wrapper.eq(ProcessFlow.NAME,processFlowParam.getName());
            Assert.eqZero(processFlowServiceBiz.count(wrapper),"工艺名称已存在");
        }
        ProcessFlow updateProcessFlow=new ProcessFlow();
        BeanUtils.copyProperties(processFlowParam,updateProcessFlow);
        setUpdateCommonField(updateProcessFlow);
        processFlowServiceBiz.updateById(updateProcessFlow);
        return successful(IdVo.builder().id(processFlow.getId()).build());
    }

    /**
     * 启用停用
     *
     * @param enabledParam 启停参数
     * @return 主键
     */
    @Override
    public R<IdVo> enabled(EnabledParam enabledParam) {
        ProcessFlow processFlow = processFlowServiceBiz.getById(enabledParam.getId());
        if(processFlow==null){
            return failed("工艺不存在");
        }
        if (!enabledParam.getEnabled().equals(processFlow.getEnabled())){
            ProcessFlow updateProcessFlow=new ProcessFlow().setId(processFlow.getId());
            updateProcessFlow.setEnabled(enabledParam.getEnabled());
            setUpdateCommonField(updateProcessFlow);
            processFlowServiceBiz.updateById(updateProcessFlow);
        }
        return successful(IdVo.builder().id(processFlow.getId()).build());
    }

    /**
     * 删除xx
     *
     * @param idParam 主键参数
     * @return 主键
     */
    @Override
    public R<IdVo> delete(IdParam idParam) {
        ProcessFlow processFlow = processFlowServiceBiz.getById(idParam.getId());
        if (processFlow==null){
            return failed("工艺不存在");
        }
        QueryWrapper<ProcessFlowEquipment> wrapper=new QueryWrapper<>();
        wrapper.eq(ProcessFlowEquipment.PROCESS_FLOW_ID,idParam.getId());
        //检查是否存在工艺详情
        if (processFlowEquipmentServiceBiz.count(wrapper)>0){
            //删除关联的工艺详情
            processFlowEquipmentServiceBiz.remove(wrapper);
        }
        QueryWrapper<ProcessFlowSubstrateRela> substrateRelaQueryWrapper =new QueryWrapper<>();
        wrapper.eq(ProcessFlowSubstrateRela.PROCESS_FLOW_ID,idParam.getId());
        //检查是否存在工艺基板关联关系
        if (processFlowSubstrateRelaServiceBiz.count(substrateRelaQueryWrapper)>0){
            //删除工艺基板关联关系
            processFlowSubstrateRelaServiceBiz.remove(substrateRelaQueryWrapper);
        }
        processFlowServiceBiz.removeById(idParam.getId());
        return successful(IdVo.builder().id(processFlow.getId()).build());
    }

    /**
     * 批量删除xx
     *
     * @param idListParam 主键列表参数
     * @return 主键列表
     */
    @Override
    public R<IdListVo> delete(IdListParam idListParam) {
        return null;
    }

/*    *//**
     * 获取基板列表
     *
     * @param
     * @return 基板列表
     *//*
    @Override
    public R<List<SubstrateVo>> substrateList() {
        List<Substrate> substrateList = substrateServiceBiz.list();
        List<SubstrateVo> substrateVos = com.mt.roll.core.utils.BeanUtils.copyList(substrateList, SubstrateVo.class);
        return successful(substrateVos);
    }

    @Override
    public R<List<BomVo>> bomList() {
        List<Bom> bomList = bomServiceBiz.list();
        List<BomVo> bomVos = com.mt.roll.core.utils.BeanUtils.copyList(bomList, BomVo.class);
        return successful(bomVos);
    }*/

    @Override
    public R<String> getCode() {

        String processFlowCode = CodeGeneratorHelper.getProcessFlowCode();
        return successful(processFlowCode);
    }
}
